<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends zulController {
	
	
	function __construct(){
		parent::__construct();
		$this->load->model("UserModel");
		
	}

	function index(){
		$this->checkSession();
		$this->load->library("form_validation");
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$this->form_validation->set_rules(array(
		array('field'=>'username','label'=>'Username','rules'=>'required'),
		array('field'=>'password','label'=>'Password','rules'=>'required'),
		));
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		if($this->form_validation->run() == FALSE){
			$this->load->view("formLogin");
		} else {
			$username	= $this->input->post("username");
			$password	= md5($this->input->post("password"));
			$result		= $this->UserModel->get_by(array('USERNAME' => $username, 'PASSWORD' => $password));
			if(!empty($result)){
				$this->session->set_userdata('id_user', $result[0]['ID_USER']);
				$this->session->set_userdata('username', $result[0]['USERNAME']);
				$this->session->set_userdata('password', $result[0]['PASSWORD']);
				$this->session->set_userdata('role', $result[0]['ROLE']);
				redirect("/Home");
			} else {
				$this->session->set_flashdata('message', '<div class="alert alert-success fade in"><button type="button" class="close" data-dismiss="alert">&times;</button>Username atau Password yang anda masukan salah</div>');
				redirect("/Login");
			}
		}
	}
	function logout(){
		$this->session->sess_destroy();  
    	$this->index();
	}
}